package com.example.fontposition.service.impl;

import cn.tedu.common.enumerator.ServiceCode;
import cn.tedu.common.ex.ServiceException;
import cn.tedu.common.pojo.vo.PageData;
import cn.tedu.zhaopin.protocol.position.vo.PositionSimpleVO;
import com.example.fontposition.dao.persest.repository.IPositionRepository;
import com.example.fontposition.dao.search.IPositionESRepository;
import com.example.fontposition.pojo.po.PositionPO;
import com.example.fontposition.pojo.vo.PositionVO;
import com.example.fontposition.service.IPositionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class PositionServiceImpl implements IPositionService {


    @Autowired
    private IPositionRepository positionRepository;

    @Autowired
    private IPositionESRepository positionESRepository;


    @Override
    public PositionVO getId(Long userId) {
        log.debug("开始处理【根据ID查询期望职位】的业务，参数：{}", userId);
        PositionVO queryResult = positionRepository.getStandardById(userId);
        if (queryResult == null) {
            String message = "查询商品失败，商品数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public void rebuildSearch() {
        positionESRepository.deleteAll();
        Integer pageNum = 1;
        Integer pageSize = 3;
        Integer maxPage;
        PageData<PositionSimpleVO> pageData;
        do {
            pageData=positionRepository.search(pageNum,pageSize);
            maxPage = pageData.getMaxPage();
            List<PositionSimpleVO> list = pageData.getList();
            positionESRepository.saveAll(list);
            pageNum++;
        } while (pageNum <= maxPage);
    }

    @Override
    public PageData<PositionSimpleVO> search(String title, Integer pageNum) {
        log.debug("开始处理【推荐职位】的业务，关键词：{}, 页码：{}，每页记录数：{}", title, pageNum);
        return positionESRepository.search(title, pageNum,8);
    }

}

